
import {LGXEnums}  from '../sysenum.js'
import lgxcom from '../common.js'

import utilfuncs from '../tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

class GradientInfo {
    constructor() {
        let red = new lgxcom.LGXColor();
        red.setVal(255, 0, 0);

        let white = new lgxcom.LGXColor();
        white.setVal(255, 255, 255);
        this.stopColorArr = [{ stop: 0, color: red }, { stop: 1, color: white }];
        this.stPt = { x: 0, y: 0 };//局部坐标系
        this.endPt = { x: 0, y: 0 };
        this.degree = 0;
        this.userSetPosFlag = false;
    }

    getCloneCopy() {
        let ret = new GradientInfo();
        ret.stopColorArr = [];
        for (let i = 0; i < this.stopColorArr.length; i++) {
            let t = this.stopColorArr[i];
            ret.stopColorArr.push(t);
        }
        ret.stPt.x = this.stPt.x;
        ret.stPt.y = this.stPt.y;

        ret.endPt.x = this.endPt.x;
        ret.endPt.y = this.endPt.y;
        ret.degree = this.degree;
        ret.userSetPosFlag = this.userSetPosFlag;
        return ret;
    }

    toJson() {
        var jsonobj = {};
        jsonobj["stopColorArr"] = this.getGradientColor();
        let tmp = { x: this.stPt.x, y: this.stPt.y };
        jsonobj["stPt"] = tmp;

        tmp = { x: this.endPt.x, y: this.endPt.y };
        jsonobj["endPt"] = tmp;
        jsonobj["degree"] = this.degree;
        jsonobj["userSetPosFlag"] = this.userSetPosFlag;
        return jsonobj;
    }
    fromJson(jsonobj) {
        if (!jsonobj) {
            return;
        }
        let p1 = jsonobj["stPt"];
        if (p1) {
            this.stPt.x = p1.x;
            this.stPt.y = p1.y;
        }
        let p2 = jsonobj["endPt"];
        if (p2) {
            this.endPt.x = p2.x;
            this.endPt.y = p2.y;
        }
        this.degree = jsonobj["degree"];
        if (!this.degree) {
            this.degree = 0;
        }
        let flag = jsonobj["userSetPosFlag"];
        if (!flag) {
            this.userSetPosFlag = false;
        }
        else {
            this.userSetPosFlag = flag;
        }
        let jsonArr = jsonobj["stopColorArr"];
        this.setStop(jsonArr);
    }

    setStopPos(subProp, pt) {
        if (subProp == "st") {
            this.stPt.x = pt.x;
            this.stPt.y = pt.y;
            this.userSetPosFlag = true;
        }
        else if (subProp == "end") {
            this.endPt.x = pt.x;
            this.endPt.y = pt.y;
            this.userSetPosFlag = true;
        }
    }

    rotate(rotCntPt, rotRad) {
        let tmpPt = funcs.rotate(this.stPt, rotCntPt, rotRad);
        this.stPt.x = tmpPt.x;
        this.stPt.y = tmpPt.y;

        tmpPt = funcs.rotate(this.endPt, rotCntPt, rotRad);
        this.endPt.x = tmpPt.x;
        this.endPt.y = tmpPt.y;
    }

    translate(tx, ty) {
        this.stPt.x += tx;
        this.stPt.y += ty;

        this.endPt.x += tx;
        this.endPt.y += ty;
    }

    setStopDefaultPos(refGraph) {
        let localZone = refGraph.getLocalZone();
        this.stPt.x = localZone.left;
        this.stPt.y = localZone.top;

        this.endPt.x = localZone.right;
        this.endPt.y = localZone.top;
        this.userSetPosFlag = true;
    }

    setStop(stopArr) {
        if (!stopArr) {
            return;
        }
        this.stopColorArr = [];
        for (let i = 0; i < stopArr.length; i++) {
            let t = stopArr[i];
            let proprotion = t.stop;
            let colorVal = t.color;
            let colorObj = new lgxcom.LGXColor();
            colorObj.setColorByValue(colorVal);
            this.stopColorArr.push({ stop: parseFloat(proprotion), color: colorObj });
        }
    }

    getGradientColor() {
        let stopArr = [];
            let arr = this.stopColorArr;
            for (let i = 0; i < arr.length; i++) {
                let t = arr[i];
                let stop = { idx: i, stop: t.stop, color: t.color.toValue() };
                stopArr.push(stop);
            }
        return stopArr;
    }
}

class LGXFillDefInfo {
    constructor() {
        this.fillStyle = LGXEnums.LGXFillStyle.EMPTY;
        this.fillColor = new lgxcom.LGXColor();
        this.fillColor.setVal(255, 0, 0);
        this.gradientInfo = null;
    }

    clone(src) {
        if (src.fillStyle){
            this.fillStyle = src.fillStyle;
        }
        this.fillColor.clone(src.fillColor);
        if (src.gradientInfo) {
            this.gradientInfo = src.gradientInfo.getCloneCopy();
        }
    }

    /*
    GradientStopItemArr:[
            { idx: 0, stop: '0',color:255},
            { idx: 1, stop: '1',color:16777215}
            ],
            */
    setStop(stopArr) {
        if (!stopArr) {
            return;
        }
        if (!this.gradientInfo) {
            this.gradientInfo = new GradientInfo();
        }
        this.gradientInfo.setStop(stopArr);
    }

    getGradientColor() {
        let stopArr = [];
        if (this.gradientInfo) {
            stopArr = this.gradientInfo.getGradientColor();
        }
        return stopArr;
    }

    toJson() {
        var jsonobj = {};
        jsonobj["fillStyle"] = this.fillStyle;
        jsonobj["fillColor"] = this.fillColor.toValue();
        if (this.gradientInfo){
            jsonobj["gradientInfo"] = this.gradientInfo.toJson();
        }
        return jsonobj;
    }
    fromJson(jsonobj) {
        if (!jsonobj) {
            return;
        }
        this.fillStyle = jsonobj["fillStyle"];
        let fillColorVal = jsonobj["fillColor"];
        if (!fillColorVal){
            fillColorVal = jsonobj["fillcolor"];
        }
        this.fillColor.setColorByValue(fillColorVal);
        let gradientObj = jsonobj["gradientInfo"];
        if (gradientObj) {
            this.gradientInfo = new GradientInfo();
            this.gradientInfo.fromJson(gradientObj);
        }
    }

    setStopPos(subProp, pt) {
        if (!pt) {
            return;
        }
        if (!pt.x || !pt.y) {
            return;
        }

        if (isNaN(pt.x) || isNaN(pt.y) || !isFinite(pt.x) || !isFinite(pt.y)) {
            return;
        }

        if (this.gradientInfo) {
            this.gradientInfo.setStopPos(subProp, pt);
        }
    }

    rotate(rotCntPt, rotRad) {
        if (this.gradientInfo) {
            this.gradientInfo.rotate(rotCntPt, rotRad);
        }
    }

    translate(tx, ty) {
        if (this.gradientInfo) {
            this.gradientInfo.translate(tx, ty);
        }
    }

    setStopDefaultPos(refGraph) {
        if (this.userSetPosFlag || !refGraph) {
            return;
        }
        if (this.gradientInfo) {
            this.gradientInfo.setStopDefaultPos(refGraph);
        }
    }


}

export default { LGXFillDefInfo, GradientInfo }
